//
//  UIButton+TitleImage.m
//  customer
//
//  Created by Holothurian on 2018/11/6.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "UIButtonTitleImage.h"

@implementation UIButton (TitleImage)

- (void) setImageVertical:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType
{
    
    CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    [self.imageView setContentMode:UIViewContentModeCenter];
    [self setImageEdgeInsets:UIEdgeInsetsMake(-self.viewHeight*0.1,
                                              0.0,
                                              0.0,
                                              -titleSize.width)];
    [self setImage:image forState:stateType];
    
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(image.size.height+self.viewHeight*0.2,
                                              -image.size.width,
                                              -self.viewHeight*0.2,
                                              0.0)];
    [self setTitle:title forState:stateType];
}


- (void)setImageHorizontalRight:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType{
    
    CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    [self.imageView setContentMode:UIViewContentModeCenter];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0,
                                              self.viewWidth * 0.1 + titleSize.width,
                                              0.0,
                                              -self.viewWidth * 0.2)];
    [self setImage:image forState:stateType];
    
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0.0,
                                              -image.size.width,
                                              0.0,
                                              self.viewWidth * 0.2)];
    [self setTitle:title forState:stateType];
    
}

@end
